package com.project.webapp.action.front.preSchool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.project.common.util.QueryUtils;
import com.project.common.util.StringUtil;
import com.project.service.front.preSchool.ISearchService;
import com.project.webapp.action.BaseAction;
import com.project.webapp.form.front.preSchool.ArticleForm;
import com.project.webapp.form.front.preSchool.SearchForm;

/**
 * 课程Search Action 实作
 * @author Sn
 *
 */
public class SearchAction extends BaseAction 
{
	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = 2508749281897601596L;
	//查询课程list信息
	private QueryUtils queryUtils = new QueryUtils();
	//实例化Form
	private SearchForm search = new SearchForm();
	//搜索标题值
	private String str = null;
	private ISearchService iSearchService;

	// 重新搜索
	public String resetsearch() 
	{
		queryUtils.setPagesize(10);
		Map arr[] = this.condition();
		application.put("key", arr);
		str = null;
		session.remove("sessionsearch");
		// 去掉默认情况下传入的title的值
		if ("请输入要搜索的内容".equals(search.getArticle_title())) 
		{
			search.setArticle_title("");
		}
		//将搜索标题的值放在筛选条件中
		str = search.getArticle_title();
		// 用来取出queryUtils中的值
		List<ArticleForm> articlelist = null;
		queryUtils.setObj(search);
		queryUtils.setLocation(new StringBuffer("search!resetsearch.action"));
		queryUtils = iSearchService.search(queryUtils);
		// 将取到的值赋予给articlelist，方便数据转化
		articlelist = queryUtils.getPagelist();
		try 
		{
			for (ArticleForm art : articlelist) 
			{
				//去掉课程信息标题过长
				art.setArticle_title(new StringUtil().substringUTF8(art.getArticle_title(), 60));
				//截取课程信息过长
				art.setArticle_content(new StringUtil().substringUTF8(art.getArticle_content(), 200)+ "...");
				//art.setTime_type("随到随开");
				art.setArea(this.type(art.getArea()));
				try 
				{
					art=this.transition(art);
				} catch (Exception e) 
				{
					art.setTime_type("一对一");
				}
				try 
				{
					// 取出对应的地区的详细分类，比如说0101
					//截取到的就赢该是01，从对应的map中取出的地区字符串就应该是江岸
					art.setArea(arr[1].get(art.getArea().substring(0, 2)).toString());
					// art=this.transition(art);
				} catch (Exception e) 
				{
					//为了避免数据出错，我们给地区一个默认的初始化值
					art.setArea("武昌洪山");
				}
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
		queryUtils.setPagelist(articlelist);

		return SUCCESS;
	}

	// 默认的搜索条件
	public String execute() throws Exception 
	{
		queryUtils.setPagesize(10);
		// 获取当前传入的搜索条件，如果存在将对应的值放入session中
		String way = decide(search);
		//去掉第一次搜索框中默认的value
		if ("请输入要搜索的内容".equals(search.getArticle_title())) 
		{
			search.setArticle_title("");
		}
		// 如果是第一次传入条件，则将搜索条件放入sesson中
		// 判断条件是判断对应的session是否有值
		if (session.get("sessionsearch") == null) 
		{
			session.put("sessionsearch", search);
		} else 
		{
			// 如果存在对应的session，我们从session中取出搜索条件
			// 然后将本次传入的搜索条件给置换掉
			SearchForm search1 = (SearchForm) session.get("sessionsearch");
			if (way == "Menu_code") 
			{
				search1.setMenu_code(search.getMenu_code());
			}
			if (way == "Area") 
			{
				search1.setArea(search.getArea());
			}
			if (way == "Course_fee") 
			{
				search1.setCourse_fee(search.getCourse_fee());
			}
			if (way == "Start_time") 
			{
				search1.setStart_time(search.getStart_time());
			}
			if (way == "Org_name") 
			{
				search1.setOrg_name(search.getOrg_name());
			}
			if (way == "Article_title") 
			{
				search1.setArticle_title(search.getArticle_title());
			}
			session.put("sessionsearch", search1);
			// 对象拷贝，第一个参数是目标对象，第一个参数是源目标
			BeanUtils.copyProperties(search, search1);
		}

		// 获取搜索栏中的条件
		Map arr[] = this.condition();
		// 将数组集合放入application中
		application.put("key", arr);
		// 提示搜索条件的判读
		str = (search.getArticle_title() == null || search.getArticle_title() == "" ? "&nbsp;" : search.getArticle_title())
			+ "&nbsp;&nbsp;"
			+ (search.getMenu_code() == null ? "&nbsp;" : (search.getMenu_code().length()==8 ? arr[0].get(search.getMenu_code()) : "") + ";")
			+ "&nbsp;&nbsp;"
			+ (search.getArea() == null || search.getArea() == "" ? "&nbsp;" : (search.getArea().length()==2 ? arr[1].get(search.getArea()) : this.type(search.getArea()))+ ";")  + "";
		
		List<ArticleForm> articlelist = null;
		// 将传入的条件放入其中
		queryUtils.setObj(search);
		// 设置分页需要跳转的路径
		queryUtils.setLocation(new StringBuffer("search.action"));
		queryUtils = iSearchService.search(queryUtils);
		// 将pagelist中的值给articlelist
		articlelist = queryUtils.getPagelist();
		// 对获取到的新闻进行处理，比如说新闻标题过长，内容过长
		for (ArticleForm art : articlelist) 
		{
			// 去掉新闻标题过长
			art.setArticle_title(new StringUtil().substringUTF8(art.getArticle_title(), 60));
			// 去掉信息过长
			art.setArticle_content(new StringUtil().substringUTF8(art.getArticle_content(), 200) + "...");
			art.setArea(this.type(art.getArea()));
			try 
			{
				art=this.transition(art);
			} catch (Exception e) 
			{
			   //System.out.println("开课时间数据转化异常");
				art.setTime_type("一对一");
			}
			/*
			try 
			{
				//有些数据个数不规范，防止程序挂掉
				art.setArea(arr[1].get(art.getArea().substring(0, 2)).toString());
			} catch (Exception e) 
			{
				//System.out.println("error：数组越界");
				e.printStackTrace();
			}
			*/
		}
		queryUtils.setPagelist(articlelist);
		
		return SUCCESS;
	}

	// 取得菜单中的导航数据
	public Map[] condition() 
	{
		Map map[] = new Map[5];
		// 课程类别
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("01010101", "胎教");
		map1.put("01010102", "亲子教育");
		map1.put("01010103", "智力开发");
		map1.put("01010104", "识字阅读");
		map1.put("01010105", "珠心算");
		map1.put("01010106", "托管陪玩");
		map1.put("01010107", "幼小衔接");
		map1.put("01010108", "其他");
		map1.put("01010201", "器乐");
		map1.put("01010202", "声乐");
		map1.put("01010203", "书法");
		map1.put("01010204", "美术");
		map1.put("01010205", "舞蹈形体");
		map1.put("01010206", "表演/主持");
		map1.put("01010207", "手工艺");
		map1.put("01010208", "棋牌");
		map1.put("01010209", "球类");
		map1.put("01010210", "武术搏击");
		map1.put("01010211", "游泳");
		map1.put("01010212", "轮滑");
		map1.put("01010213", "其他");
		map1.put("01010230", "英语");
		// 上课地点
		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("01", "江岸");
		map2.put("02", "江汉");
		map2.put("03", "桥口");
		map2.put("04", "汉阳");
		map2.put("05", "武昌");
		map2.put("06", "青山");
		map2.put("07", "洪山");
		map2.put("08", "东西湖");
		map2.put("09", "汉南");
		map2.put("10", "江夏");
		map2.put("11", "蔡甸");
		map2.put("12", "黄陂");
		map2.put("13", "新洲");
		// 机构名称
		Map<String, String> map3 = new HashMap<String, String>();
		map3.put("11", "巨龙教育");
		map3.put("12", "清华教育");
		map3.put("13", "其他");
		// 开课时间
		Map<String, String> map4 = new HashMap<String, String>();
		map4.put("11", "一周内");
		map4.put("12", "两周内");
		map4.put("13", "两周之后");
		map4.put("14", "两周之后");
		map4.put("05", "已经结束");
		// 收费标准
		Map<String, String> map5 = new HashMap<String, String>();
		map5.put("11", "1000元以下");
		map5.put("12", "1000元-2500元");
		map5.put("13", "2500元-5000元");
		map5.put("04", "5000元以上");
		map[0] = map1;
		map[1] = map2;
		map[2] = map3;
		map[3] = map4;
		map[4] = map5;
		return map;
	}

	// 判断当前传入的搜索条件，返回当前选择的搜索条件
	public String decide(SearchForm search) 
	{
		String way = null;
		if (search.getMenu_code() != null && !search.getMenu_code().equals("")) 
		{
			way = "Menu_code";
		}
		if (search.getArea() != null && !search.getArea().equals("")) 
		{
			way = "Area";
		}
		if (search.getCourse_fee() != null && !search.getCourse_fee().equals("")) 
		{
			way = "Course_fee";
		}
		if (search.getStart_time() != null && !search.getStart_time().equals("")) 
		{
			way = "Start_time";
		}
		if (search.getOrg_name() != null && !search.getOrg_name().equals("")) 
		{
			way = "Org_name";
		}
		if (search.getArticle_title() != null && !search.getArticle_title().equals("")) 
		{
			way = "Article_title";
		}
		return way;
	}

	// 进行数据格式化操作
	private ArticleForm transition(ArticleForm art) 
	{
		StringBuffer time_type = new StringBuffer();
		int i = 0;
		for (String stu : art.getTime_type().split("；")) 
		{
			if ("0".equals(stu)) 
			{
				time_type.append(i > 0 ? "、随叫随到" : "随叫随到");
			} else if ("1".equals(stu)) 
			{
				time_type.append(i > 0 ? "、一周内" : "一周内");
			} else if ("2".equals(stu)) 
			{
				time_type.append(i > 0 ? "、两周内" : "两周内");
			} else if ("3".equals(stu)) 
			{
				time_type.append(i > 0 ? "、一个月内" : "一个月内");
			} else if ("4".equals(stu)) 
			{
				time_type.append(i > 0 ? "、上课中" : "上课中");
			} else if ("5".equals(stu)) 
			{
				time_type.append(i > 0 ? "、已结束" : "已结束");
			}
			i++;
		}
		i = 0;
		art.setTime_type(time_type.toString());

		return art;
	}
	
	//详细地址获取
	public String type(String str)
	{
		Map<String, String>map=new HashMap<String, String>();
			//江岸区
			map.put("0101","江岸一元");
			map.put("0102","江岸大智");
			map.put("0103","江岸车站");
			map.put("0104","江岸四唯");
			map.put("0105","江岸球场");
			map.put("0106","江岸西马");
			map.put("0107","江岸永清");
			map.put("0108","江岸劳动");
			map.put("0109","江岸台北");
			map.put("0110","江岸花桥");
			map.put("0111","江岸二七");
			map.put("0112","江岸新村");
			map.put("0113","江岸丹水池");
			map.put("0114","江岸后湖");
			map.put("0115","江岸塔子湖");
			map.put("0116","江岸谌家矶");
			map.put("0117","江岸经济开发区");
			//江汉区
			map.put("0201","江汉民族");
			map.put("0202","江汉民权");
			map.put("0203","江汉满春");
			map.put("0204","江汉花楼");
			map.put("0205","江汉水塔");
			map.put("0206","江汉前进");
			map.put("0207","江汉民意");
			map.put("0208","江汉新华");
			map.put("0209","江汉北湖");
			map.put("0210","江汉唐家墩");
			map.put("0211","江汉长青");
			map.put("0212","江汉万松");
			map.put("0213","江汉汉兴");
			map.put("0214","江汉江汉经济开发区");
			//硚口区
			map.put("0301","硚口易家墩");
			map.put("0302","硚口韩家墩");
			map.put("0303","硚口宗关");
			map.put("0304","硚口汉水桥");
			map.put("0305","硚口宝丰");
			map.put("0306","硚口古田");
			map.put("0307","硚口汉中");
			map.put("0308","硚口荣华");
			map.put("0309","硚口六角亭");
			map.put("0310","硚口汉正");
			map.put("0311","硚口长丰");
			map.put("0312","硚口开发区");
			//汉阳区
			map.put("0401","汉阳洲头");
			map.put("0402","汉阳晴川");
			map.put("0403","汉阳月湖");
			map.put("0404","汉阳翠微");
			map.put("0405","汉阳建桥");
			map.put("0406","汉阳鹦鹉");
			map.put("0407","汉阳五里墩");
			map.put("0408","汉阳琴断口");
			map.put("0409","汉阳六角亭");
			map.put("0410","汉阳永丰");
			map.put("0411","汉阳江堤");
			map.put("0412","汉阳经济开发区");
			//武昌区
			map.put("0501","武昌民族");
			map.put("0502","武昌民权");
			map.put("0503","武昌满春");
			map.put("0504","武昌花楼");
			map.put("0505","武昌水塔");
			map.put("0506","武昌前进");
			map.put("0507","武昌民意");
			map.put("0508","武昌新华");
			map.put("0509","武昌北湖");
			map.put("0510","武昌唐家墩");
			map.put("0511","武昌长青");
			map.put("0512","武昌万松");
			map.put("0213","武昌汉兴");
			map.put("0214","武昌");
			//青山区
			map.put("0601","青山红钢城");
			map.put("0602","青山新沟桥");
			map.put("0603","青山红卫路");
			map.put("0604","青山冶金");
			map.put("0605","青山钢花村");
			map.put("0606","青山青山");
			map.put("0607","青山武东");
			map.put("0608","青山白玉山");
			map.put("0609","青山厂前");
			map.put("0610","青山工人村");
			map.put("0611","江岸钢都管理委");
			//洪山区
			map.put("0701","洪山珞南");
			map.put("0702","洪山关山");
			map.put("0703","洪山梨园");
			map.put("0704","洪山狮子山");
			map.put("0705","洪山和平");
			map.put("0706","洪山洪山");
			map.put("0707","洪山张家湾");
			map.put("0708","洪山青菱");
			map.put("0709","洪山卓刀泉");
			map.put("0710","洪山天兴乡");
			//东西湖
			map.put("0801","东西湖吴家山");
			map.put("0802","东西湖走马岭");
			map.put("0803","东西湖新沟");
			map.put("0804","东西湖长青");
			map.put("0805","东西湖慈惠");
			map.put("0806","东西湖泾河");
			map.put("0807","东西湖金银湖");
			map.put("0808","东西湖将军路");
			map.put("0809","东西湖东山");
			map.put("0810","东西湖柏泉");
			map.put("0811","东西湖辛安渡");
			map.put("0812","东西湖常青花园新区");
			//汉南区
			map.put("0901","汉南纱帽");
			map.put("0902","汉南湘口");
			map.put("0903","汉南汉南农场");
			map.put("0904","汉南银莲湖农场");
			map.put("0905","汉南东荆");
			map.put("0906","汉南乌金农场");
			map.put("0907","汉南东城垸农场");
			map.put("0908","汉南邓南");
			map.put("0909","汉南开发区");
			//江夏
			map.put("1001","江夏纸坊");
			map.put("1002","江夏金口");
			map.put("1003","江夏郑店");
			map.put("1004","江夏乌龙泉");
			map.put("1005","江夏法泗");
			map.put("1006","江夏安山");
			map.put("1007","江夏湖泗");
			map.put("1008","江夏五里界");
			map.put("1009","江夏山坡乡");
			map.put("1010","江夏舒安乡");
			map.put("1011","江夏梁子湖风景区");
			map.put("1012","江夏金水");
			map.put("1013","江夏庙山");
			map.put("1014","江夏藏龙岛");
			map.put("1015","江夏大桥新区");
			//蔡甸区
			map.put("1101","蔡甸蔡甸");
			map.put("1102","蔡甸张湾");
			map.put("1103","蔡甸侏儒");
			map.put("1104","蔡甸永安");
			map.put("1105","蔡甸奓山");
			map.put("1106","蔡甸大集");
			map.put("1107","蔡甸索河");
			map.put("1108","蔡甸玉贤");
			map.put("1109","蔡甸消泗乡");
			map.put("1110","蔡甸桐湖");
			map.put("1111","蔡甸军山");
			map.put("1112","蔡甸沌口");
			//黄陂区
			map.put("1201","黄陂三里");
			map.put("1202","黄陂横店");
			map.put("1203","黄陂六指");
			map.put("1204","黄陂蔡店乡");
			map.put("1205","黄陂滠口");
			map.put("1206","黄陂大潭");
			map.put("1207","黄陂祁家湾");
			map.put("1208","黄陂蔡家榨");
			map.put("1209","黄陂李家集");
			map.put("1210","黄陂长轩岭");
			map.put("1211","黄陂姚家集");
			map.put("1212","黄陂罗汉寺");
			map.put("1213","黄陂木兰乡");
			map.put("1214","黄陂前川");
			map.put("1215","蔡甸王家河");
			map.put("1216","蔡甸武湖 ");
			map.put("1217","蔡甸天河");
			map.put("1218","蔡甸木兰山风景管理区");
			map.put("1219","蔡甸盘龙城经济发展区");
			//新洲区
			map.put("1301","新洲邾城");
			map.put("1302","新洲旧");
			map.put("1303","新洲潘塘");
			map.put("1304","新洲阳逻");
			map.put("1305","新洲双柳");
			map.put("1306","新洲三店");
			map.put("1307","新洲仓埠");
			map.put("1308","新洲汪集");
			map.put("1309","新洲李集");
			map.put("1310","新洲徐古");
			map.put("1311","新洲凤凰");
			map.put("1312","新洲辛冲");
			if(map.get(str)!=null)
			{
				return map.get(str);
			}else
			{			
				return "洪山街道口";
			}
	}
	
	//详细菜单
	public String menu(String menu)
	{	
		return "";
	}

	public QueryUtils getQueryUtils() 
	{
		return queryUtils;
	}

	public void setQueryUtils(QueryUtils queryutils) 
	{
		this.queryUtils = queryutils;
	}
	
	public SearchForm getSearch() 
	{
		return search;
	}

	public void setSearch(SearchForm search) 
	{
		this.search = search;
	}

	public ISearchService getiSearchService() 
	{
		return iSearchService;
	}

	public void setiSearchService(ISearchService iSearchService) 
	{
		this.iSearchService = iSearchService;
	}

	public String getStr() 
	{
		return str;
	}

	public void setStr(String str) 
	{
		this.str = str;
	}

}
